package com.huahai.security.core.validate.code.mobile.sms;

import com.huahai.security.core.properties.SecurityConstants;
import com.huahai.security.core.properties.StatusCodeEnum;
import com.huahai.security.core.supports.ResultResponse;
import com.huahai.security.core.validate.code.ValidateCode;
import com.huahai.security.core.validate.code.impl.AbstractValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Jun
 * @Date: 2020/7/16 下午4:33
 *
 * 短信验证码处理器
 */
@Component("smsValidateCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    /**
     * 短信验证码发送器
     */
    @Autowired
    private SmsCodeSender smsCodeSender;

    /**
     * 重写 父类的抽象发送方法
     * @param request
     * @param response
     * @param validateCode
     * @return
     * @throws Exception
     */
    @Override
    protected ResultResponse send(HttpServletRequest request, HttpServletResponse response, ValidateCode validateCode) throws Exception {
        String paramName = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;
        String mobile = ServletRequestUtils.getRequiredStringParameter(request, paramName);
        smsCodeSender.send(mobile, validateCode.getCode());

        Map<String,Object> map = new HashMap<>(2);
        map.put("uuid", validateCode.getUuid());
//        map.put("validateCode", validateCode.getCode());
        return ResultResponse.build(StatusCodeEnum.HSC200000.getCode(),StatusCodeEnum.HSC200000.getMessage(),map);
    }

}
